package org.jeecg.modules.demo.ia2bill.service.impl;

import org.jeecg.modules.demo.ia2bill.entity.IaI2bil;
import org.jeecg.modules.demo.ia2bill.entity.IaI2bilB;
import org.jeecg.modules.demo.ia2bill.mapper.IaI2bilBMapper;
import org.jeecg.modules.demo.ia2bill.mapper.IaI2bilMapper;
import org.jeecg.modules.demo.ia2bill.service.IIaI2bilService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 入库单
 * @Author: jeecg-boot
 * @Date:   2024-03-11
 * @Version: V1.0
 */
@Service
public class IaI2bilServiceImpl extends ServiceImpl<IaI2bilMapper, IaI2bil> implements IIaI2bilService {

	@Autowired
	private IaI2bilMapper iaI2bilMapper;
	@Autowired
	private IaI2bilBMapper iaI2bilBMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(IaI2bil iaI2bil, List<IaI2bilB> iaI2bilBList) {
		iaI2bilMapper.insert(iaI2bil);
		if(iaI2bilBList!=null && iaI2bilBList.size()>0) {
			for(IaI2bilB entity:iaI2bilBList) {
				//外键设置
				entity.setIdH(iaI2bil.getId());
				iaI2bilBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(IaI2bil iaI2bil,List<IaI2bilB> iaI2bilBList) {
		iaI2bilMapper.updateById(iaI2bil);
		
		//1.先删除子表数据
		iaI2bilBMapper.deleteByMainId(iaI2bil.getId());
		
		//2.子表数据重新插入
		if(iaI2bilBList!=null && iaI2bilBList.size()>0) {
			for(IaI2bilB entity:iaI2bilBList) {
				//外键设置
				entity.setIdH(iaI2bil.getId());
				iaI2bilBMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		iaI2bilBMapper.deleteByMainId(id);
		iaI2bilMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			iaI2bilBMapper.deleteByMainId(id.toString());
			iaI2bilMapper.deleteById(id);
		}
	}
	
}
